/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Operator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class OperatorService extends GenericService<Operator> {

    public OperatorService() {
        super(Operator.class);
    }

    public List<Operator> findOperator(Operator op) {
        String sql = "select o from Operator o WHERE ";

        if ((op.getName() != null) && (!op.getName().equals(""))) {
            sql += " lower(o.name) like lower(:name) and  ";
        }
//        if ((!op.getNumReg().equals("")) && (op.getNumReg() != null)) {
//            sql += " lower(o.numReg) like lower(:num) and  ";
//        }
        if ((op.getAns() != null) && (!op.getAns().equals(""))) {
            sql += " lower(o.ans) like lower(:ans) and  ";
        }
        sql += "1=1";

        Query query = getEntityManager().createQuery(sql);

        if ((op.getName() != null) && (!op.getName().equals(""))) {
            query.setParameter("name", "%" + op.getName() + "%");
        }
//        if ((!op.getNumReg().equals("")) && (op.getNumReg() != null)) {
//            query.setParameter("num", "%" + op.getNumReg() + "%");
//        }
        if ((op.getAns() != null) && (!op.getAns().equals(""))) {
            query.setParameter("ans", "%" + op.getAns() + "%");
        }
        return query.getResultList();

    }

    public List<Operator> autocomplete(String pro) {
        String sql = "select o from Operator o WHERE ";

        if ((pro != null) && (!pro.equals(""))) {
            sql += " lower(o.name) like lower(:name) and  ";
        }

        sql += "1=1 Order by o.name ASC ";

        Query query = getEntityManager().createQuery(sql);

        if ((pro != null) && (!pro.equals(""))) {
            query.setParameter("name", "%" + pro + "%");
        }
        System.err.println(query.getResultList().size());
        return query.getResultList();

    }

    public Long countOperator() {
        String sql = "select count(o) from Operator o ";

        Query query = getEntityManager().createQuery(sql);

        return (Long) query.getSingleResult();

    }
}
